let HttpManager = module.exports = {};
var timeout = 8000;

HttpManager.get = function(url,data,succCallBack,errCallBack){
    if (data == null) {
        data = {};
    }
    data.token = cc.sys.localStorage.getItem('token');

    let xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status == 200) {
            let respone = xhr.responseText;
            let rsp = JSON.parse(respone);
            if (rsp.code == 0){
                errCallBack && errCallBack(rsp);
                return;
            }
            if (rsp.code == 1){
                succCallBack(rsp);
                return;
            }
        } else if (xhr.readyState === 4 && xhr.status == 401) {
            let respone = xhr.responseText;
            let rsp = JSON.parse(respone);
            createMsgBox(rsp.msg,function(){
                Global.NoticeManager.emitMessage("UpdateToken", false);
            },function(){
                cc.game.end()
            });
            errCallBack && errCallBack(rsp.msg);
        }else{
            // let respone = xhr.responseText;
            // let rsp = JSON.parse(respone);
            // createMsgBox(`${rsp.msg},错误码：${rsp.code}`);
            // errCallBack(rsp.msg);
        }
    };
    
    let httpurl =  `${Global.Config.baseUrl}${url}?${Global.Utils.queryParams(data)}`;
    xhr.withCredentials = false;
    xhr.open('GET', httpurl, true);
    xhr.timeout = timeout;
    
    xhr.ontimeout  = function (e) {
        errCallBack&&errCallBack('请求超时！');
    };
    xhr.onerror  = function () {
        errCallBack&&errCallBack('网络错误！');
    };
    xhr.send();
};

HttpManager.post = function(url,data,succCallBack,errCallBack){
    if (data == null) {
        data = {};
    }
    data.token = cc.sys.localStorage.getItem('token');

    let xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status == 200) {
            let respone = xhr.responseText;
            let rsp = JSON.parse(respone);
            if (rsp.code == 0){
                errCallBack && errCallBack(rsp);
                return;
            }
            if (rsp.code == 1){
                succCallBack(rsp);
                return;
            }
        } else if (xhr.readyState === 4 && xhr.status == 401) {
            let respone = xhr.responseText;
            let rsp = JSON.parse(respone);
            createMsgBox(rsp.msg,function(){
                Global.NoticeManager.emitMessage("UpdateToken", false);
            },function(){
                cc.game.end()
            });
            errCallBack && errCallBack(rsp.msg);
        }else{
            // let respone = xhr.responseText;
            // let rsp = JSON.parse(respone);
            // createMsgBox(`${rsp.msg},错误码：${rsp.code}`);
            // errCallBack(rsp.msg);
        }
    };
    
    let httpurl = Global.Config.baseUrl + url;
    xhr.withCredentials = false;
    xhr.open('POST', httpurl, true);
    xhr.setRequestHeader("Content-Type","application/json;charset=UTF-8");
    xhr.timeout = timeout;
    xhr.ontimeout  = function (e) {
        errCallBack&&errCallBack('请求超时！');
    };
    xhr.onerror  = function () {
        errCallBack&&errCallBack('网络错误！');
    };
    xhr.send(JSON.stringify(data));
}